#include "ParseMP3.h"

int parse_mp3(char* filePath,struct Meta* ret_m)
{
	FILE* fp;

	
	int i = 0;

	unsigned char TAG[3]={0};
	unsigned char Version[2]={0};
	unsigned char Flags[1]={0};
	unsigned char TagSize[4]={0};

	unsigned char UPPER_BIT[2]={0};
	unsigned char DOWN_BIT[2] = {0};
	
	int sizeBytes[4]={0};

	int duration = 0;

	int BitRate=0;
	int Frequency;
	int FrameSize = 0;


	long size = 0;

	long fullsize=0;

	


	fp = fopen(filePath,"rb");
	

	ret_m->upnpClass=(char*)malloc(strlen(UPNP_AUDIO)+1);
	strcpy(ret_m->upnpClass,UPNP_AUDIO);

	ret_m->ProtocolInfo = (char*)malloc(strlen(PROTOCOL_MP3)+1);
	strcpy(ret_m->ProtocolInfo,PROTOCOL_MP3);


	
	
	ret_m->fileSize = GetFileLength(fp);




	fread(TAG,sizeof(unsigned char),(size_t)3,fp);

	if(strncmp(TAG,"ID3",3) != 0)
	{
		ID_old(fp);
	}else
	{

		fread(Version,sizeof(unsigned char),(size_t)2,fp);
		fread(Flags,sizeof(unsigned char),(size_t)1,fp);
		fread(TagSize,sizeof(unsigned char),(size_t)4,fp);


		for(i = 0 ; i < 4 ; i++)
		{
			sizeBytes[i] = sizeBytes[i] | ( (int)TagSize[i] & 0x7F) ;
		}

		for(i = 0 ; i < 4 ; i++)
		{
			size = (size<<7) | sizeBytes[i] ;
		}

		switch(Version[0])
		{
		case 2:
			ID3v2(fp,ret_m);
			break;
		case 3:
			ID3v3(fp,ret_m);
			break;
		case 4:
			ID3v4(fp,ret_m);
			break;
		}
	

		getBitrate(fp,size+10,&BitRate, &Frequency,&ret_m->layer);


		fseek(fp,0L,SEEK_END);
		fullsize = ftell(fp);


		size = fullsize - size;


		FrameSize = (144 * BitRate * 1000) / (Frequency+1);


		duration = (size * 8 ) / (BitRate*1000);
		
		
		ret_m->Frequency = Frequency;
		ret_m->Bitrate = BitRate;
		ret_m->Duration = duration;

	}


	fclose(fp);
	return FILE_AUDIO;
}


int getBitrate(FILE* f, long skip,int* Bitrate, int* Frequency, int* Layer)
{

	int bitrateTable[3][16]={{0, 32, 40,48,56,64,80,96,112,128,160,192,224,256,320,-1 },{0,32,48,56,64,80,96,112,128,160,192,224,256,320,384,-1},{0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,-1}};

	int LayerCode[]={0,3,2,1};
	int FreqTable[]={44100,48000,32000,0};

	int freqIDX=0;

	int FreqPartial = 0;

	unsigned char MP_SyncHeader[2]={0};
	unsigned char MP_FrameHeader[3]={0};


	unsigned char MISC[6]={0};

	unsigned char version=0;

	


	fseek(f,skip,SEEK_SET);

	fread(MP_SyncHeader,sizeof(unsigned char),(size_t)2,f);
	fread(MISC,sizeof(unsigned char),(size_t)6,f);


	version = (int)MP_SyncHeader[1] & 0x0f;

	*Layer = ((int)version & 0x06) >> 1;


	*Frequency = FreqTable[((int)MISC[0] & 0x0f)>>2];
	*Bitrate =  bitrateTable[*Layer-1][(int)MISC[0]>>4];

	*Layer = LayerCode[*Layer];

}

int ID3v2(FILE* fp, struct Meta* ret_m){
	unsigned char FrameHeader[4]={0};
	unsigned char FrameSize[2]={0};
	unsigned char FLAG[2]={0};
	unsigned char* FrameValue;

	int i =0 ;
	int cnt = 0;



	do{
		fread(FrameHeader,sizeof(unsigned char),(size_t)4,fp);
		fread(FrameSize,sizeof(unsigned char),(size_t)2,fp);
		fread(FLAG,sizeof(unsigned char),(size_t)1,fp);

		FrameValue = (unsigned char*)malloc(sizeof(unsigned char) * getIntFromByte(FrameSize,2)-1);
		fread(FrameValue,sizeof(unsigned char),(size_t)(getIntFromByte(FrameSize,2)-1),fp);


		if(strncmp(FrameHeader,"TT2",4) == 0)
		{

			ret_m->title = (char*)malloc(strlen(FrameValue)+1);
			strcpy(ret_m->title,FrameValue);

			cnt++;
		}



		free(FrameValue);
		
	
		if(cnt >= 3)
			break;

	}while(i++ < 5);

}

int ID3v3(FILE* fp, struct Meta* ret_m){
	
	unsigned char FrameHeader[4]={0};
	unsigned char FrameSize[4]={0};
	unsigned char FLAG[2]={0};
	unsigned char* FrameValue;

	char TITLE[256]={0};

	int i =0 ;
	int size= 0;


	do{
		fread(FrameHeader,sizeof(unsigned char),(size_t)4,fp);
		fread(FrameSize,sizeof(unsigned char),(size_t)4,fp);
		fread(FLAG,sizeof(unsigned char),(size_t)2,fp);

		size = getIntFromByte(FrameSize,4);

		FrameValue = (unsigned char*)malloc(sizeof(unsigned char) * size);
		fread(FrameValue,sizeof(unsigned char),(size_t)size,fp);

		if(strncmp(FrameHeader,"TIT2",4) == 0)
		{

			getStringFromBuffer(FrameValue,TITLE,size);

			ret_m->title = (char*)malloc(strlen(TITLE)+1);
			strcpy(ret_m->title,TITLE);

			break;
		}
		else if(strncmp(FrameHeader,"TOPE",4) == 0 )
		{

		}
		else if(strncmp(FrameHeader,"TPE1",4) == 0 )
		{

		}



		free(FrameValue);
		
	

	}while(i++ < 50);

}

int ID3v4(FILE* fp,struct Meta* ret_m){
	unsigned char FrameHeader[4]={0};
	unsigned char FrameSize[4]={0};
	long F_Size=0;
	unsigned char FLAG[2]={0};
	unsigned char* FrameValue;
	int sizeBytes[4]={0};
	int i = 0;

	char TITLE[256]={0};


	int cnt = 0;
	
	do{
		
		fread(FrameHeader,sizeof(unsigned char),(size_t)4,fp);
		fread(FrameSize,sizeof(unsigned char),(size_t)4,fp);
		fread(FLAG,sizeof(unsigned char),(size_t)2,fp);
		
		for(i = 0 ; i < 4 ; i++)
		{
			sizeBytes[i] = 0;
		}

		for(i = 0 ; i < 4 ; i++)
		{
			sizeBytes[i] = sizeBytes[i] | ( (int)FrameSize[i] & 0x7F) ;
		}

		F_Size = 0;
		for(i = 0 ; i < 4 ; i++)
		{
			F_Size = (F_Size<<7) | sizeBytes[i] ;
		}

		FrameValue = (unsigned char*)malloc(sizeof(unsigned char) * F_Size);
		fread(FrameValue,sizeof(unsigned char),(size_t)F_Size,fp);
	
		if(strncmp(FrameHeader,"TIT2",4) == 0)
		{

			getStringFromBuffer(FrameValue,TITLE,F_Size);

			ret_m->title = (char*)malloc(strlen(TITLE)+1);
			strcpy(ret_m->title,TITLE);
			break;
		}
		else if(strncmp(FrameHeader,"TOPE",4) == 0 )
		{
			

		}
		else if(strncmp(FrameHeader,"TPE1",4) == 0 )
		{

		}else if(strncmp(FrameHeader,"TRCK",4) == 0 )
		{
			
		}



		free(FrameValue);
		

	}while(cnt++ < 2);

}

int ID_old(FILE* f){

}


